﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using TMDT_Nhom1A.BUS;
using TMDT_Nhom1A.DTO;
using System.Drawing;

namespace TMDT_Nhom1A.UserControl_Manager
{
    public partial class RegisterWebserviceManager : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                gvRegisterWebserviceManager.DataSource = RegisterWebserviceBUS.getAllRegisterWebservice();
                gvRegisterWebserviceManager.DataBind();
            }
        }

        protected void gvRegisterWebserviceManager_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lbStatus = (Label)e.Row.FindControl("lbstatus");
                Label lbinformation = (Label)e.Row.FindControl("lbinformation");
                CheckBox cbCheck = (CheckBox)e.Row.FindControl("cbcheck");

                if (lbStatus.Text == "True")
                {
                    lbinformation.Text = "Đã Duyệt";
                    cbCheck.Visible = false;
                }
                else
                {
                    
                    lbinformation.Text = "Chưa Duyệt";
                }

                Label lbKichHoat = (Label)e.Row.FindControl("lbKichHoat");
                CheckBox cbKichHoat = (CheckBox)e.Row.FindControl("cbKichHoat");
                if (lbKichHoat.Text == "0")
                {
                    cbKichHoat.Checked = false;
                }
                else
                {
                    cbKichHoat.Checked = true;
                }
              
            }
            
        }

        protected void gvRegisterWebserviceManager_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "CheckAll")
            {
                CheckBox cbcheckall = (CheckBox)gvRegisterWebserviceManager.HeaderRow.FindControl("cbCheckAll");
                if (cbcheckall.Checked == true)
                {
                    for (int i = 0; i < gvRegisterWebserviceManager.Rows.Count; i++)
                    {
                        CheckBox cbcheck = (CheckBox)gvRegisterWebserviceManager.Rows[i].FindControl("cbcheck");
                        cbcheck.Checked = false;
                        cbcheckall.Checked = false;
                    }
                }
                else
                {
                    for (int i = 0; i < gvRegisterWebserviceManager.Rows.Count; i++)
                    {
                        CheckBox cbcheck = (CheckBox)gvRegisterWebserviceManager.Rows[i].FindControl("cbcheck");
                        cbcheck.Checked = true;
                        cbcheckall.Checked = true;
                    }
                }
            }
        }

        protected void btactive_Click(object sender, EventArgs e)
        {
             HttpContext context = HttpContext.Current;
             string path = String.Format("{0}://{1}/", context.Request.Url.Scheme, context.Request.Url.Host);

            for (int i = 0; i < gvRegisterWebserviceManager.Rows.Count; i++)
            {
                CheckBox cbcheck = (CheckBox)gvRegisterWebserviceManager.Rows[i].FindControl("cbcheck");
                Label registerWebservice = (Label)gvRegisterWebserviceManager.Rows[i].Cells[6].FindControl("lbRegisterID");
                if (cbcheck.Checked == true)
                {
                    
                    string email = gvRegisterWebserviceManager.Rows[i].Cells[2].Text;
                    
                    //set thuoc tinh active lai bang true va gui mail
                    bool result = RegisterWebserviceBUS.activateRegisterWebservice(int.Parse(registerWebservice.Text));
                    if (result == true)
                    {
                        //send mail
                        bool sendMailResult = utils.User.sendMailToMuaChung(email, path);
                        if (sendMailResult == true)
                        {
                            LbResult.Text = "Duyệt và Gởi Mail Thành Công";
                            LbResult.ForeColor = Color.Blue;
                        }
                        else
                        {
                            LbResult.Text = "Gủi mail không thành công";
                            LbResult.ForeColor = Color.Blue;
                        }

                        
                    }
                    else
                    {
                        LbResult.Text = "Không Thành Công";
                        LbResult.ForeColor = Color.Red;
                    }
                   
                }

                //tim cbkich hoat de set trang thai la cho phep su dung webservice hay ko
                CheckBox cbKichHoat = (CheckBox)gvRegisterWebserviceManager.Rows[i].FindControl("cbKichHoat");
                bool KichHoatResult = false;
                if (cbKichHoat.Checked == true)
                {
                    KichHoatResult = RegisterWebserviceBUS.allowOrNotAllowUsingRegisterWebservice(int.Parse(registerWebservice.Text), 1);
                }
                else
                {
                    KichHoatResult = RegisterWebserviceBUS.allowOrNotAllowUsingRegisterWebservice(int.Parse(registerWebservice.Text), 0);
                }

                if (KichHoatResult == false)
                {
                    LbResult.Text = "Không Thành Công";
                    LbResult.ForeColor = Color.Red;
                    break;
                }
                

            }

            gvRegisterWebserviceManager.DataSource = RegisterWebserviceBUS.getAllRegisterWebservice();
            gvRegisterWebserviceManager.DataBind();
        }
    }
}